package com.qf.qfcenterservice.api;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qf.common.feigns.DeviceTopicApi;
import com.qf.common.model.QfDeviceTopic;
import com.qf.qfcenterservice.service.DeviceTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class DeviceTopicFeignApi implements DeviceTopicApi {

    @Autowired
    private DeviceTopicService deviceTopicService;

    @GetMapping("/api/deviceTopic/topics")
    @Override
    public List<String> findAllTopics() {

        List<QfDeviceTopic> deviceTopics = deviceTopicService.list();
        List<String> topics = deviceTopics.stream()
                //将QfDeviceTopic类型的流转换成String类型的流
                .map(deviceTopic -> deviceTopic.getTopic())
                //将流信息收集到List集合
                .collect(Collectors.toList());

        return topics;
    }

    @Override
    public List<String> findTopicByDeviceKey(String deviceKey) {
        List<QfDeviceTopic> deviceTopics = deviceTopicService.list(Wrappers.lambdaQuery(QfDeviceTopic.class).eq(QfDeviceTopic::getDeviceKey, deviceKey));
        return deviceTopics.stream()
                //过滤出所有上报数据的主题
                .filter(topic->{
                    return topic.getTopic().startsWith("qfjava/device/data)");
                })
                //将QfDeviceTopic类型的流转成String类型的流
                .map(topic->topic.getTopic())
                //将String类型的流转成List返回
                .collect(Collectors.toList());
    }
}
